/*
Copyright (C) Ariff Ehsan, Abdul Hadi Fikri

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*  Author : Ariff Ehsan
 *           Abdul Hadi Fikri
 *  Date   : June 18, 2009
 */


#include <system.h>

/**
*Interface at16_timer0{
*	Provide{
*		command AT16_TIMER0_Init();                @DRIVER_CLOCK_INIT();
*		command AT16_TIMER0_reg(void(*fired)())    @DRIVER_CLOCK_REG(x);
*	}
*	Uses{
*		signal void(*at16_timer0_fired)();
*	}
*}
*
*Data Field:
*/	void (*atmega16_timer0_fired)(void);	
	
/**
*Function Member	
*/	void ATMEGA16_TIMER0_defaultFired(void);

/********************************************************************************************/
/*                                  Clients Function Call                                   */
/********************************************************************************************/
void ATMEGA16_TIMER0_init(void) {
	// set default fired pointer
	atmega16_timer0_fired = ATMEGA16_TIMER0_defaultFired;
	
	// set CTC mode and prescaller 64
	TCCR0 = (1<<WGM01)|(1<<CS01)|(1<<CS00);
	TCNT0 = 0;
	
	// set timer count for 1 milli seconds
	OCR0 = ATMEGA16_TIMER_OC;
	
	// enable output compare 0 interrupt
	TIMSK |= (1<<OCIE0);
}

void ATMEGA16_TIMER0_reg(void (*fired)(void)) {
	if (fired != NULL)
		atmega16_timer0_fired = fired;
}
/********************************************************************************************/
/*                                 Internal Function Call                                   */
/********************************************************************************************/
void ATMEGA16_TIMER0_defaultFired(void) {
}
/********************************************************************************************/
/*                              HW/SW Interrupt Function Call                               */
/********************************************************************************************/
ISR(TIMER0_COMP_vect) {

	FLIP_BIT(CLOCK_PULSE_PORT, CLOCK_PULSE_PIN);

	(*atmega16_timer0_fired)();
}
